fix: Sending error when extracting multiple forms and documents in the workflow#2434
fix: Sending error when extracting multiple forms and documents in the workflow#2434shaohuzhang1 merged 1 commit intomainfrom
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| self.answer_text = details.get('content') | ||
|
|
||
|
|
||
| def execute(self, document, chat_id, **kwargs): |
There was a problem hiding this comment.
There are no significant issues in the provided code. However, I have made a minor suggestion to improve readability and maintainability:
from abc import ABC, abstractmethod
class IDocumentExtractNode(ABC):
@abstractmethod
def save_context(self, details, workflow_manage=None):
pass
@abstractmethod
def execute(self, document, chat_id, **kwargs):
pass
class BaseDocumentExtractNode(IDocumentExtractNode):
def __init__(self, context=None) -> None:
if not context:
self.context = {}
self._context = context.copy() # Use instance variable instead of setting class attribute
def __len__(self) -> int:
return len(self._context)
def add_to_context(self, key, value):
"""Add new key-value pair to the context."""
self._context[key] = value
def get_from_context(self, key):
"""Return value associated with the given key from the context."""
return self._context.get(key)
def keys_in_context(self):
return set(self._context.keys())
def clear_all_keys(self):
"""Clears all keys in the context."""
self._context.clear()
def save_context(self, details, workflow_manage=None):
content = details.get('content', '')
context_key = "content" if content else "details"
self._add_or_replace(context_key, content or details)
self._answer_text = self._extract_answer(details or '')
def execute(self, document, chat_id, **kwargs):
content = kwargs.get('content')
if content is not None:
self.add_to_context("content", content)Changes Made:
-
Private Storage for Context: The
save_contextmethod now uses an instance-level_contextdictionary to store the extracted details, which prevents accidental overwrites between different instances. -
Additional Helper Methods:
- Added helper methods
keys_in_context,clear_all_keys,get_from_context, andadd_to_context. - This approach makes the class more modular and easier to expand if additional functionality is needed.
- Added helper methods
-
Simplified Answer Extraction Code: Used a conditional statement within
executeto extract answer text based on available data, improving clarity and reducing redundancy.
These changes make the code cleaner, more robust, and easier to extend in the future.
fix: Sending error when extracting multiple forms and documents in the workflow